import { Component } from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {PublicProvider} from "../../providers/public/public";
import {SceneRoomChoosePage} from "../scene-room-choose/scene-room-choose";

/**
 * Generated class for the ScenePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-scene',
  templateUrl: 'scene.html',
})
export class ScenePage {
  scenes =[];
  scene_list = [];

  rooms = ['全部房间'];
  room_list = [];
  choosed_room:any;

  constructor(public navCtrl: NavController, public navParams: NavParams,public modalCtrl: ModalController,private pub: PublicProvider) {
  }

  ionViewDidLoad() {
    this.scenes = this.pub.scene_list;
    this.scene_list = this.scenes;
    this.set_room();
  }


  set_room(){
    for(let i=0;i<this.scene_list.length;i++){
      if(this.rooms.indexOf(this.scene_list[i].position) < 0  && this.scene_list[i].position != '' ){
        this.rooms.push(this.scene_list[i].position);
      }else {
        continue;
      }
    }

    if(this.rooms.length <= 6){
      this.room_list = this.rooms;
    } else {
      this.room_list = this.rooms.slice(0,6);
    }
    this.choosed_room = this.room_list[0];
  }

  choose_room(n){
    this.choosed_room = this.room_list[n];
    if(n == 0){
      this.scene_list = this.scenes;
    } else {
      this.scene_list = [];
      for(let s of this.scenes){
        if(s.position === this.choosed_room ){
          this.scene_list.push(s);
        }
      }
    }
  }

  all_room(){
    let profileModal = this.modalCtrl.create(SceneRoomChoosePage, { room: this.rooms });
    profileModal.onDidDismiss(data => {
      console.log(data);
    });
    profileModal.present();
  }

  take_on_off(n) {
    if(this.scene_list[n].state == 0) {
      this.scene_list[n].state = 1;
    } else {
      this.scene_list[n].state = 0;
    }
  }

}
